package cn.edu.cqu.fredyvia.Count;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class CountReducer extends Reducer<Text, IntWritable, NullWritable, Text> {

    //reduce输出的key-value对格式:term <doc1,num1>...<total,sum>
    @Override
    public void reduce(Text key, Iterable<IntWritable> values, Context context)
            throws IOException, InterruptedException {
        try {
            String[] word = key.toString().split("#");
            System.out.println("word:" + word);
            Integer count = 0;
            for (IntWritable val : values) {
                count += 1;
            }
            context.write(NullWritable.get(), new Text(word[0] + "\t" + word[1] + "\t" + count));
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(-1);
        }
    }

    @Override
    protected void cleanup(Context context) throws IOException, InterruptedException {

        super.cleanup(context);
    }
}


